package com.sgb.goods.view.adapter.publish;

import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsItemBrandBinding;
import com.sgb.goods.entity.BrandEntity;
import com.sgb.goods.utils.StringUtils;
import com.sgb.goods.utils.base.adapter.RecordPointCommonAdapter;

/**
 * 品牌列表适配器
 * @author cyj
 */
public class BrandAdapter extends RecordPointCommonAdapter<BrandEntity> {

    public BrandAdapter() {
        super(R.layout.goods_item_brand, BR.viewModel);
    }

    @Override
    protected void convert(BaseViewHolder baseHolder, ViewDataBinding dataBinding, BrandEntity entity) {
        GoodsItemBrandBinding mDataBinding = (GoodsItemBrandBinding) dataBinding;
        RecordPointCommonAdapter<BrandEntity.BrandChild> brandChildAdapter = new RecordPointCommonAdapter<BrandEntity.BrandChild>(R.layout.goods_item_brand_child, BR.viewModel) {
            @Override
            public boolean equivalentToObject(BrandEntity.BrandChild object1, BrandEntity.BrandChild object2) {
                if(null == object1 || null == object2 || StringUtils.isEmpty(object1.id)) {
                    return false;
                }
                return object1.id.equals(object2.id);
            }
        };
        mDataBinding.childList.setAdapter(brandChildAdapter);
        brandChildAdapter.setNewData(entity.childList);
        brandChildAdapter.setRecordPoint(getCurrentObj());
        brandChildAdapter.setItemClickListener((adapter, view, position) -> {
            this.currentObj = entity;
            this.position = position;
        });
    }

    @Override
    public boolean equivalentToObject(BrandEntity object1, BrandEntity object2) {
        return false;
    }

    /**
     * 此处根据不同的业务做不同的实现。
     * 对于多级数据来讲，由于只有第一级的适配器是随外部布局的生命周期影响，因此或者历史item一定只能在第一级适配器来处理
     * @return    根据不同业务，拼接一个对象(当前或者下级层级等等)给外部调用者使用
     */
    public BrandEntity.BrandChild getCurrentObj() {
        if(null != this.oldObj && -1 != this.position) {
            return this.oldObj.childList.get(this.position);
        }
        return null;
    }

}
